<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <!-- <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div> -->
      <img src="@/assets/logo.595745bd.png" alt="logo" class="login-logo">
      <el-form-item prop="username">
        <span class="svg-container">
          <span class="iconfont icon-shouji" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          prop="phone"
          placeholder="请输入账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <!-- <svg-icon icon-class="password" /> -->
          <span class="iconfont icon-mima" />
        </span>
        <el-input
          ref="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          :type="type"
        />
        <span class="show-pwd" icon-class="eye">
          <svg-icon :icon-class="type? 'eye':'eye-open'" @click="watchPwd" />

        </span>
      </el-form-item>
      <el-form-item prop="code">
        <el-row>
          <el-col :span="17">
            <span class="svg-container">
              <span class="iconfont icon-yanzhengyanzhengma" />
            </span>
            <el-input
              ref="code"
              v-model="loginForm.code"
              placeholder="请输入验证码"
              name="code"
              type="text"
              tabindex="3"
              auto-complete="on"
              prop="code"
            />
          </el-col>
          <el-col :span="7">
            <img id="canvas_img" :src="`/api/user-service/user/imageCode/${loginForm.clientToken}`" alt="code" class="code-img">
            <!-- < img :src="`/api/user-service/user/imageCode/${loginForm.clientToken}`" alt="code" class="code-img" @click="getCodeImg"> -->
          </el-col>
        </el-row>
      </el-form-item>

      <el-button type="primary" style="width:100%;margin-bottom:30px;" class="login-btn" @click="loginFn">登录</el-button>

    </el-form>
  </div>
</template>
<script>
// import { valiPhone } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    // const checkPhone = (rule, value, callback) => {
    //   if (valiPhone(value)) {
    //     callback()
    //   } else {
    //     callback(new Error('手机号格式错误'))
    //   }
    // }
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: Math.floor(Math.random() * 100 + 1),
        loginType: 0
      },
      loginRules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' }
          // { pattern: checkPhone, message: '手机格式错误', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '最短6最长16', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }

        ]
      },
      type: 'password'
    }
  },

  methods: {
    watchPwd() {
      this.type = this.type === 'password' ? '' : 'password'
    },
    loginFn() {
      // console.log(111)
      this.$refs.loginForm.validate(async(vali) => {
        if (vali) {
          // console.log(vali)
          try {
            this.loading = true
            await this.$store.dispatch('user/loginApi', this.loginForm)
          } finally {
            this.loading = false
          }

          this.$router.push('/')
        }
      })
    }

  }

}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#999;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .login-logo{
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
    height: 47px;
    background: transparent;
    border: 0;
    border-radius: 0;
    padding: 12px 5px 12px 15px;
    color: #999;
    caret-color: #999;
    -webkit-appearance: none;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $cursor inset !important;
        -webkit-text-fill-color: $bg !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #e2e2e2;
    background-color:#fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background:  url('~@/assets/background.jpg') no-repeat 0 0 / cover;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0;
    background: #fff;
    -webkit-box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    box-shadow: 0 3px 70px 0 rgb(30 111 72 / 35%);
    border-radius: 10px;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
.login-btn {
  width: 100%;
    height: 52px;
    background: linear-gradient(262deg,#2e50e1,#6878f0);
    opacity: .91;
    border-radius: 8px;
    color: #fff;
    text-shadow: 0 7px 22px #cfcfcf;
}
.code-img{
  display: block;
  height: 53px;
  width: 100%;
}
</style>
